package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("travel")
public class TravelController {

    @Value("${file.dir}")
    private String dir;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private ITravelCommentService travelCommentService;




    @RequestMapping("")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        //pageInfo
        model.addAttribute("pageInfo",travelService.query(qo));
        return "travel/list";
    }

    @RequiredLogin
    @RequestMapping("input")
    public String input(Long id,Model model) {
        if(id!=null){
            //不需要查询文章内容,跳转页面后发ajax查询.
            model.addAttribute("tv",travelService.get(id));
        }
        //dests
        model.addAttribute("dests",destinationService.listDests());
        return "travel/input";
    }

    @RequestMapping("getContent")
    @ResponseBody
    public Object getContent(Long id) {

        return  travelService.getContent(id);
    }

//游记封面上传.
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public String coverImageUpload(MultipartFile pic){
        //直接返回上传文件的名称
        return UploadUtil.upload(pic,dir);
    }

    @RequiredLogin
    @ResponseBody   //不贴,页面没有跳转
    @RequestMapping("/saveOrUpdate")
    public  Object saveOrUpdate(Travel travel,@LoginUser UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        //设置当前用户
        travel.setAuthor(userInfo);
//        result.setData(travel.getId());  返回攻略.
        return travelService.saveOrUpdate(travel);
    }




    @RequestMapping("detail")
    public String detail(Long id, Model model) {
    //评论list
        //游记内容
        Travel travel=travelService.get(id);
        travel.setTravelContent(travelService.getContent(id));
        model.addAttribute("detail",travel);
        //toasts
        Long destId = travel.getDest().getId();
        List<Destination> toasts = destinationService.getToasts(destId);
        model.addAttribute("toasts", toasts);

        //sds 攻略 查询该目的地下点击前3的攻略文章
        model.addAttribute("sds", detailService.listViewnumTop3(destId));

//      ts 游记 查询该目的地下带你计量前3的游记
        model.addAttribute("ts", travelService.listViewnumTop3(destId));

        //list 当前游记下的所有评论
        model.addAttribute("list", travelCommentService.listTavelId(id));


        return "travel/detail";
    }

    //评论
    @RequiredLogin
    @RequestMapping("commentAdd")
    public Object commentAdd(TravelComment entity,Integer floor
             ,@LoginUser UserInfo user,Model model){
        //填充用户信息
        entity.setUserId(user.getId());
        entity.setUsername(user.getNickname());
        entity.setLevel(user.getLevel());
        entity.setHeadUrl(user.getHeadImgUrl());
        entity.setCity(user.getCity());
        travelCommentService.save(entity);
        //c 游记评论对象
         model.addAttribute("c",entity);
         //floor楼层数
         model.addAttribute("floor",floor+1);
        return "travel/commentTpl";
    }



    //UME上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        java.lang.String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, dir);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }




}
